package com.ruoyi.member.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Consumer;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.db.domain.SourceMemberDataInfoHistory;
import com.ruoyi.db.mapper.MemberSourceDictInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.MemberSourceInfoMapper;
import com.ruoyi.db.domain.MemberSourceInfo;
import com.ruoyi.member.service.IMemberSourceInfoService;

/**
 * 用户资质信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-13
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class MemberSourceInfoServiceImpl implements IMemberSourceInfoService {
    @Autowired
    private MemberSourceInfoMapper memberSourceInfoMapper;
    @Autowired
    private MemberSourceDictInfoMapper memberSourceDictInfoMapper;

    /**
     * 查询用户资质信息
     *
     * @param uid 用户资质信息主键
     * @return 用户资质信息
     */
    @Override
    public MemberSourceInfo selectMemberSourceInfoByUid(Long uid) {
        return memberSourceInfoMapper.selectMemberSourceInfoByUid(uid);
    }

    @Override
    public List<Map<String, Object>> selectMemberSourceInfoByChannelCode(MemberSourceInfo memberSourceInfo) {
        return memberSourceInfoMapper.selectMemberSourceInfoByChannelCode(memberSourceInfo);
    }

    @Override
    public List<Map<String, Object>> selectAgeByChannelCode(MemberSourceInfo memberSourceInfo) {
        return memberSourceInfoMapper.selectAgeByChannelCode(memberSourceInfo);
    }

    @Override
    public List<Map<String, Object>> selectProvinceByChannelCode(MemberSourceInfo memberSourceInfo) {
        return memberSourceInfoMapper.selectProvinceByChannelCode(memberSourceInfo);
    }

    @Override
    public List<Map<String, Object>> getCity(MemberSourceInfo memberSourceInfo) {
        List<Map<String, Object>> cityCount = memberSourceInfoMapper.getCity(memberSourceInfo);
        return cityCount;
    }

    @Override
    public List<Map<String, Object>> getCityList(MemberSourceInfo memberSourceInfo) {
        List<Map<String, Object>> cityCount = memberSourceInfoMapper.getCityCount(memberSourceInfo);
        Map<String, List<Map<String, Object>>> provinceCityMap = new HashMap<>();

        for (Map<String, Object> city : cityCount) {
            String province = (String) city.get("province");
            String cityName = (String) city.get("city");
            long value = (Long) city.get("value");

            if (provinceCityMap.containsKey(province)) {
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("name", cityName);
                cityMap.put("value", value);

                provinceCityMap.get(province).add(cityMap);
            } else {
                List<Map<String, Object>> cityList = new ArrayList<>();
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("name", cityName);
                cityMap.put("value", value);

                cityList.add(cityMap);
                provinceCityMap.put(province, cityList);
            }
        }

        List<Map<String, Object>> transformedData = new ArrayList<>();
        for (Map.Entry<String, List<Map<String, Object>>> entry : provinceCityMap.entrySet()) {
            List<Map<String, Object>> cityData = entry.getValue();

            Map<String, Object> provinceMap = new HashMap<>();
            provinceMap.put("name", entry.getKey());
            provinceMap.put("children", cityData);

            transformedData.add(provinceMap);
        }

        return transformedData;
    }


    @Override
    public List<Map<String, Object>> selectMemberSourceInfoMapList(MemberSourceInfo memberSourceInfo) {
        String orFilter = memberSourceInfo.getOrFilter();
        if(orFilter==null){
            return memberSourceInfoMapper.selectMemberSourceInfoMapList(memberSourceInfo);
        }
        if ("none000".equals(orFilter)) {
            memberSourceInfo.setHouse("hu001");
            memberSourceInfo.setCar("car001");
            memberSourceInfo.setGjj("gjj001");
            memberSourceInfo.setShebao("sb001");
            memberSourceInfo.setBaoxian("bx001");
            memberSourceInfo.setXinyongka("xyk001");
        }

        if("yq000".equals(orFilter)){
            memberSourceInfo.setYuqi("yq001");
        }
        Map<String, Consumer<String>> fieldSetters = new HashMap<>();
        fieldSetters.put("hu002", v -> memberSourceInfo.setHouse("hu002"));
        fieldSetters.put("car002", v -> memberSourceInfo.setCar("car002"));
        fieldSetters.put("gjj002", v -> memberSourceInfo.setGjj("gjj002"));
        fieldSetters.put("sb002", v -> memberSourceInfo.setShebao("sb002"));
        fieldSetters.put("bx002", v -> memberSourceInfo.setBaoxian("bx002"));
        fieldSetters.put("xyk002", v -> memberSourceInfo.setXinyongka("xyk002"));

        fieldSetters.forEach((key, setter) -> {
            if (orFilter.contains(key)) {
                setter.accept(key);
            }
        });

        List<Map<String, Object>> list = memberSourceInfoMapper.selectMemberSourceInfoMapList(memberSourceInfo);


//        if (!list.isEmpty()) {
//            // 获取所有要查询的字段名
//            List<String> fieldNames = Arrays.asList("house", "loanAmount", "car", "gjj", "shebao", "baoxian", "zhima",
//                    "huabei", "weili", "baitiao", "yuqi", "xueli", "gongzuo", "xinyongka", "loanTime", "shouru", "yongtu");
//
//            for (Map<String, Object> map : list) {
//                // 为每个对象维护独立的映射
//                Map<String, String> fieldNameToNameMap = new HashMap<>();
//                for (String fieldName : fieldNames) {
//                    String value = (String) map.get(fieldName);
//                    if (StringUtils.isNotEmpty(value)) {
//                        String name = memberSourceDictInfoMapper.selectNameByValue(value);
//                        fieldNameToNameMap.put(fieldName, name);
//                    }
//                }
//
//                // 遍历数据列表，将字段值替换为名称
//                for (String fieldName : fieldNames) {
//                    String value = (String) map.get(fieldName);
//                    if (StringUtils.isNotEmpty(value) && fieldNameToNameMap.containsKey(fieldName)) {
//                        map.put(fieldName, fieldNameToNameMap.get(fieldName));
//                    }
//                }
//            }
//        }
        return list;
    }

    @Override
    public List<Map<String, Object>> selectMemberSourceInfoCardList(MemberSourceInfo memberSourceInfo) {
        List<Map<String, Object>> list = memberSourceInfoMapper.selectMemberSourceInfoCardList(memberSourceInfo);

        if (!list.isEmpty()) {
            // 获取所有要查询的字段名
            List<String> fieldNames = Arrays.asList("house", "loanAmount", "car", "gjj", "shebao", "baoxian", "zhima",
                    "huabei", "weili", "baitiao", "yuqi", "xueli", "gongzuo", "xinyongka", "loanTime", "shouru", "yongtu");

            for (Map<String, Object> map : list) {
                // 为每个对象维护独立的映射
                Map<String, String> fieldNameToNameMap = new HashMap<>();
                for (String fieldName : fieldNames) {
                    String value = (String) map.get(fieldName);
                    if (StringUtils.isNotEmpty(value)) {
                        String name = memberSourceDictInfoMapper.selectNameByValue(value);
                        fieldNameToNameMap.put(fieldName, name);
                    }
                }

                // 遍历数据列表，将字段值替换为名称
                for (String fieldName : fieldNames) {
                    String value = (String) map.get(fieldName);
                    if (StringUtils.isNotEmpty(value) && fieldNameToNameMap.containsKey(fieldName)) {
                        map.put(fieldName, fieldNameToNameMap.get(fieldName));
                    }
                }
            }
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> selectZhiMaList1(MemberSourceInfo memberSourceInfo) {

        return  memberSourceInfoMapper.selectZhiMaList1(memberSourceInfo);
    }

    @Override
    public String selectAllZhiMaList(MemberSourceInfo memberSourceInfo) {

        return  memberSourceInfoMapper.selectAllZhiMaList(memberSourceInfo);
    }


    @Override
    public List<Map<String, Object>> selectZhiMaList(Integer type) {
        LocalDate dateInfo = LocalDate.now();

        if (type == 0 || type == 1) {
            dateInfo = (type == 0) ? dateInfo : dateInfo.minusDays(1); // 今天或者昨天的日期
        } else if (type == 2 || type == 3) {
            int date = (type == 2) ? 3 : 5; // 根据类型确定要减去的天数
            LocalDate startDate = dateInfo.minusDays(date); // 开始日期
            LocalDate endDate = LocalDate.now().minusDays(1);
            List<Map<String, Object>> mapList = memberSourceInfoMapper.selectAllzhima(startDate.toString(), endDate.toString());
            Long number = memberSourceInfoMapper.countAllumber(startDate.toString(), endDate.toString());

            Map<String, Object> totalMap = new HashMap<>();
            totalMap.put("name", "全国");
            totalMap.put("number", number);
            mapList.add(0, totalMap);

            return mapList;

        }else if (type == 4) {
            LocalDate startDate = LocalDate.now().minusDays(30); // 开始日期
            LocalDate endDate = LocalDate.now().minusDays(1); // 结束日期

            List<Map<String, Object>> mapList = memberSourceInfoMapper.selectAllzhima(startDate.toString(), endDate.toString());
            Long number = memberSourceInfoMapper.countAllumber(startDate.toString(), endDate.toString());
            Map<String, Object> totalMap = new HashMap<>();
            totalMap.put("name", "全国");
            totalMap.put("number", number);
            mapList.add(0, totalMap);

            return mapList;
        }

        Long number = memberSourceInfoMapper.countAuthNumber(dateInfo.toString());
        List<Map<String, Object>> mapList = memberSourceInfoMapper.selectzhima(dateInfo.toString());
        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("name", "全国");
        totalMap.put("number", number);
        mapList.add(0, totalMap);
        return mapList;
    }


    /**
     * 新增用户资质信息
     *
     * @param memberSourceInfo 用户资质信息
     * @return 结果
     */
    @Override
    public int insertMemberSourceInfo(MemberSourceInfo memberSourceInfo) {
        memberSourceInfo.setCreateTime(DateUtils.getNowDate());
        return memberSourceInfoMapper.insertMemberSourceInfo(memberSourceInfo);
    }

    /**
     * 修改用户资质信息
     *
     * @param memberSourceInfo 用户资质信息
     * @return 结果
     */
    @Override
    public int updateMemberSourceInfo(MemberSourceInfo memberSourceInfo) {
        memberSourceInfo.setUpdateTime(DateUtils.getNowDate());
        return memberSourceInfoMapper.updateMemberSourceInfo(memberSourceInfo);
    }

    /**
     * 批量删除用户资质信息
     *
     * @param uids 需要删除的用户资质信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberSourceInfoByUids(Long[] uids) {
        return memberSourceInfoMapper.deleteMemberSourceInfoByUids(uids);
    }

    /**
     * 删除用户资质信息信息
     *
     * @param uid 用户资质信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberSourceInfoByUid(Long uid) {
        return memberSourceInfoMapper.deleteMemberSourceInfoByUid(uid);
    }

    @Override
    public Long countNumberByDate(String startDate, String endDate) {
        return memberSourceInfoMapper.countNumberByDate(startDate, endDate);
    }

    @Override
    public List<Map<String, Object>> upload(List<String> md5List) {
        return memberSourceInfoMapper.upload(md5List);
    }
}
